
package projecteuler.problems.palindromic36;

/**
 *
 * @author CJ
 */
public class Palindronmic {
    public static void run(){
        int current = 1;
        int total = 0;
        while(current < 1000000){
            if(intPallidrone(current) && bytePallidrone(toByteBool(current))){
                total+= current;
            }
            current++;
        }

        System.out.println(total);
    }

    public static boolean bytePallidrone(boolean[] input){
        for(int i = 0; i < input.length/2; ++i){
            if(!((input[i] && input[input.length - 1 - i]) ||
                    (!input[i] && !input[input.length - 1 - i])))
            {
                return false;
            }
        }
        return true;
    }

    public static boolean intPallidrone(int input){
        int tenPow = 0;
        while(Math.pow(10, tenPow) <= input){
            tenPow++;
        }

        int[] intArray = new int[tenPow];
        int currentIndex = 0;
        while(input != 0){
            intArray[currentIndex] = input % 10;
            input /= 10;
            currentIndex++;
        }

        //printArray(intArray);

        for(int i = 0;i < intArray.length/2; ++i){
            if(!(intArray[i] == intArray[intArray.length - 1 - i])){
                return false;
            }
        }

        return true;
    }

    public static void printArray(int[] input){
        for(int i = input.length - 1; i > -1;--i){
            System.out.print(input[i]);
        }
        System.out.println();
    }

    public static void printArray(boolean[] input){
        for(int i = input.length - 1; i > -1; --i){
            int printed = (input[i])?1:0;
            System.out.print(printed);
        }
        System.out.println();
    }

            
    public static boolean[] toByteBool(int input) {
        int twoPow = 0;
        while(Math.pow(2, twoPow) <= input){
            twoPow++;
        }

        boolean[] result = new boolean[twoPow];
        int currentIndex = result.length - 1;
        while(input != 0){
            if(Math.pow(2, currentIndex) <= input){
                result[currentIndex] = true;
                input -= Math.pow(2, currentIndex);
            }else{
                result[currentIndex] = false;
            }
            currentIndex--;
        }

        return result;
    }
}
